<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Job;
use App\Models\Company;
use App\Models\Jobcate;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;

class JobController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Job(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('jobtitle');
            $grid->column('worktype')->display(function ($value){
                return (new Jobcate())->getJobCateById( $value) ?? '';
            });
            $grid->column('salary');
            $grid->column('sex');
            $grid->column('num');
            $grid->column('age');
            $grid->column('education');
            $grid->column('express');
            $grid->column('companyid')->display(function ($value){
                return (new Company())->getNameById($value) ?? '';
            });
            $grid->column('special')->display(function ($value){
                $a = '';
                $len = count($value);
                if ($len>0){
                    for ($i=0;$i<$len;$i++){
                        if ($i==$len-1){
                            $a.= $value[$i];
                        }else{
                            $a.=$value[$i].',';
                        }
                    }
                }
                return $a;
            });
            $grid->column('content');
            $grid->column('is_hot','是否热门')->switch();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Job(), function (Form $form) {
            $form->display('id');
            $form->text('jobtitle');
            $form->select('worktype')->options('getJobCate')->options(function ($id){
                if ($id){
                    $province = Jobcate::where('name',$id)->first();
                    if ($province){
                        return [$province->id => $province->name];
                    }
                    else{
                        return [0 => '请选择'];
                    }
                }else{
                    return [0 => "请选择"];
                }
            });
            $form->text('salary');
            $form->radio('sex')->options([0 => '男', 1=> '女',3=>'不限'])->default(3);
            $form->text('num');
            $form->text('age');
            $form->select('education')->options(config('setting.education'));
            $form->select('express')->options(config('setting.express'));
            $form->checkbox('special')->options(config('setting.special'))->saving(function ($value) {
                    // 转化成json字符串保存到数据库
                    return json_encode($value);
            });
            $form->select('companyid')->options('getAllCompany');
            $form->text('content');
            $form->switch('is_hot','是否最火');
        });
    }
}
